<?php

	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}

	include_once 'letter_pdf.php';

	
	
	if( !isset($_GET["appointmentNo"]) || preg_match("/^\d+$/", $_GET["appointmentNo"]) == false ){
		echo "<script>alert('No appointment no find!');window.location.href='index.php';</script>";
	}
		
	$ini_array = parse_ini_file("config/config.ini");
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	date_default_timezone_set($ini_array["default_timezone"]);
		
	$appointmentNo = mysqli_real_escape_string($conn,$_GET["appointmentNo"]);
	$getInfoSql = "SELECT visit.`Patient_no`,visit.`Datetime_patient_seen`, visit.`signs`, visit.`symptoms`, visit.`diagnosis`, visit.`treatment`, visit.`comments`,
	clinic.`Clinic_detail`, patient.`First_name`, patient.`Surname`, patient.`address`, patient.`Date_birth`,doctor.`Doctor_no`, doctor.`doctor_first_name`, doctor.`doctor_surname`,
	doctor.`doctor_title`, doctor.`doctor_address`, doctor.`known_as`, appointment.isFirstVisit
	FROM visit LEFT OUTER JOIN patient ON visit.`Patient_no` = patient.`Patient_no`
	LEFT OUTER JOIN clinic ON visit.`Clinic_no`=clinic.`Clinic_no`
	LEFT OUTER JOIN doctor ON patient.doctor_no=doctor.`Doctor_no`,
	appointment WHERE visit.Appointment_no = appointment.`appointment_no` AND  visit.`Appointment_no`= $appointmentNo";
		
		
	
		
		
	$infoResult = mysqli_query($conn, $getInfoSql);
		
		
	$patientNo = 0;
	$datetimeSeen = "";
	$signs = "";
	$symtoms = "";
	$diagnosis = "";
	$treatment = "";
	$comments = "";
	$clinicDetail = "";
	$patientFirstName = "";
	$patientSurname = "";
	$patientAddress = "";
	$doctorNo = 0;
	$doctorFirstName = "";
	$doctorSurname = "";
	$doctorTitle = "";
	$doctorAddress = "";
	$isFirstVisit = 0;
	$patientBirth ="";
	$knownAs = "";
		
	if( $row = mysqli_fetch_array($infoResult) ){
		$patientNo = $row["Patient_no"];
		$datetimeSeen = date("d/m/Y", strtotime($row["Datetime_patient_seen"]));
		$signs = iconv('UTF-8', 'ISO-8859-1', $row["signs"]);
		$symtoms = iconv('UTF-8', 'ISO-8859-1', $row["symptoms"]);
		$diagnosis = iconv('UTF-8', 'ISO-8859-1', $row["diagnosis"]);
		$treatment = iconv('UTF-8', 'ISO-8859-1', $row["treatment"]);
		$comments = iconv('UTF-8', 'ISO-8859-1', $row["comments"]);
		$clinicDetail = iconv('UTF-8', 'ISO-8859-1', $row["Clinic_detail"]);
		$patientFirstName = iconv('UTF-8', 'ISO-8859-1', $row["First_name"]);
		$patientSurname = iconv('UTF-8', 'ISO-8859-1', $row["Surname"]);
		$patientAddress = iconv('UTF-8', 'ISO-8859-1', $row["address"]);
		$doctorNo = $row["Doctor_no"];
		$doctorFirstName = iconv('UTF-8', 'ISO-8859-1', $row["doctor_first_name"]);
		$doctorSurname = iconv('UTF-8', 'ISO-8859-1', $row["doctor_surname"]);
		$doctorTitle = iconv('UTF-8', 'ISO-8859-1', $row["doctor_title"]);
		$doctorAddress = iconv('UTF-8', 'ISO-8859-1', $row["doctor_address"]);
		$isFirstVisit = $row["isFirstVisit"];
		$patientBirth = date("d-m-Y", strtotime($row["Date_birth"]));
		$knownAs = iconv('UTF-8', 'ISO-8859-1', $row["known_as"]);
	}else{
		echo "<script>alert('this appointment no has no visit record!!!');window.location.href='index.php'</script>";
	}
	
	
	$fontStr = "Times";
	
	
	$pdf = new letter_pdf();
	$pdf->AddPage();
	
	
	
	$pdf->SetLineWidth("0.2");
	$pdf->SetDrawColor(0, 0, 0);
	$pdf->Rect(8, 10, 190, 30, "D");
	$pdf->Rect(9, 11, 80, 28, "D");
	$pdf->Rect(91, 11, 106, 28, "D");
	
	$pdf->SetFont($fontStr, "B");
	$pdf->SetFontSize(16);
	$pdf->Text(21, 20, "Dr. Geraldine Morrow");
	$pdf->Text(18, 26, "Consultant Dermatologist");
	$pdf->SetFont($fontStr);
	$pdf->SetFontSize(8);
	$pdf->Text(24, 31, "M.B., B.Ch., B.A.O., M.R.C.P.I.");
	$pdf->Text(22, 38, "Medical Council  Reg. No. 009981");
	
	$pdf->SetFont($fontStr, "BI");
	$pdf->SetFontSize(10);
	$pdf->Text(93, 20, "All Correspondence to: ");
	$pdf->Text(93, 24, "Phone : ");
	$pdf->Text(93, 28, "e-mail: ");
	
	$pdf->SetFont($fontStr, "");
	$pdf->Text(135, 20, "39 Grosvenor Road,  Rathmines,  D6");
	$pdf->Text(135, 24, "01-4970392          Fax.:  01-4974119");
	$pdf->SetFont($fontStr, "U");
	$pdf->Text(135, 28, "dermatology.secretary@gmail.com");
	
	$pdf->setFont($fontStr, "B");
	$pdf->Text(105, 38, "Practice Website :");
	
	$pdf->setFont($fontStr, "U");
	$pdf->Text(140, 38, "www.geraldinemorrow.com");
	
	$pdf->setFont($fontStr, "");
	$pdf->Text(27, 60, "To:".$doctorTitle." ".$doctorFirstName." ".$doctorSurname.",");
	

	
	
	//$pdf->AddUniGBhwFont('uGB');
	//$pdf->AddFont('uGB');
	//$pdf->Text(10, 10, "encodeing=".mb_detect_encoding($doctorSurname));

	
	$pdf->SetY(56);
	//$pdf->Line(27, 60, 185, 60);
	//$pdf->Line(27, 0, 185, 0);
	//$pdf->Line(27, 90, 185, 90);
	$addressSplit = explode(',', $doctorAddress);
	$length = count($addressSplit);
	for($i = 0; $i < $length; $i++ ){
		$pdf->SetXY(32, $pdf->GetY() + 6);
		if( $i < 4 ){
			if( $i == ($length - 1) ){
				$pdf->Cell(65, 6, trim($addressSplit[$i]));
			}else{
				$pdf->Cell(65, 6, trim($addressSplit[$i].","));
			}
		}else{
			break;
		}
		
	}
	//$pdf->Text(46, 69, $doctorAddress);
	
	$pdf->Text(120, 60, "Re:".$patientFirstName." ".$patientSurname);
	$pdf->SetY(56);
	$addressSplit = explode(',', $patientAddress);
	$length = count($addressSplit);
	for($i = 0; $i < $length; $i++ ){
		$pdf->SetXY(125, $pdf->GetY() + 6);
		if( $i < 4 ){
			if( $i == ($length - 1) ){
				$pdf->Cell(65, 6, trim($addressSplit[$i]));
			}else{
				$pdf->Cell(65, 6, trim($addressSplit[$i].","));
			}
		}else{
			break;
		}
		
	}
	$pdf->SetY(103);
	$pdf->Text(22, $pdf->GetY(), "D.O.B.:".$patientBirth);

	$pdf->SetY($pdf->GetY() + 6);
	$pdf->Text(22, $pdf->GetY(), "Ref.No. ".$patientNo);
	$pdf->Text(149, $pdf->GetY(), "Date Printed:".date("d-m-Y"));

	$pdf->SetY($pdf->GetY() + 9);
	$pdf->Text(22, $pdf->GetY(), "Dear ".$knownAs.",");
	$pdf->SetY($pdf->GetY() + 4);
	if( $isFirstVisit == 1 ){
		$pdf->Text(28, $pdf->GetY(), "I saw ". $patientFirstName." on ".$datetimeSeen.".");
	}else{
		$pdf->Text(28, $pdf->GetY(), "I reviewed ". $patientFirstName." on ".$datetimeSeen.".");
	}

	$pdf->SetY($pdf->GetY() + 8);
	$pdf->SetLineWidth("0.4");
	$pdf->Line(22, $pdf->GetY(), 185, $pdf->GetY());

	$pdf->SetY($pdf->GetY() + 6);
	$pdf->Text(22, $pdf->GetY(), "MEDICAL DETAILS");
	$pdf->Text(22, $pdf->GetY(), "MEDICAL DETAILS");
	$pdf->SetY($pdf->GetY() + 6);
	$pdf->Text(22, $pdf->GetY(), "Symptoms :");
	
	$pdf->SetXY(22,$pdf->GetY() + 2);
	$pdf->SetLeftMargin(22);
	$pdf->SetRightMargin(22);
	$pdf->write(5, $symtoms, "");
	
	$pdf->SetLineWidth("0.2");
	$pdf->Line(22, $pdf->GetY() + 8, 185, $pdf->GetY() + 8);
	$pdf->Text(22, $pdf->GetY() + 12, "Signs:");
	
	$pdf->SetXY(22, $pdf->GetY() + 14);
	$pdf->Write(5, $signs);
	
	$pdf->Line(22, $pdf->GetY() + 8, 185, $pdf->GetY() + 8);
	$pdf->Text(22, $pdf->GetY() + 12, "Diagnosis:");
	$pdf->SetXY(22, $pdf->GetY() + 14);
	$pdf->Write(5, $diagnosis);
	
	$pdf->Line(22, $pdf->GetY() + 8, 185, $pdf->GetY() + 8);
	$pdf->Text(22, $pdf->GetY() + 12, "Treatment:");
	$pdf->SetXY(22, $pdf->GetY() + 14);
	$pdf->Write(5, $treatment);
	
	//$pdf->Line(22, 272, 185, 272);
	
	$pdf->Line(22, $pdf->GetY() + 8, 185, $pdf->GetY() + 8);
	$pdf->Text(22, $pdf->GetY() + 12, "Comments:");
	$pdf->SetXY(22, $pdf->GetY() + 14);
	$pdf->Write(5, $comments);
	
	
	
	
	$pdf->SetLineWidth("0.4");
	$pdf->Line(22, $pdf->GetY() + 8, 185, $pdf->GetY() + 8);
	
	if( ($pdf->GetY() + 40 + 25) > 272  ){
		if( ($pdf->GetY() + 8 + 4 + 25) < 272 ){
			$pdf->Image("./picture/signature.png", 70, $pdf->GetY() + 8 + 4, 75, 25);
		}else{
			$pdf->AddPage();
			$pdf->Image("./picture/signature.png", 70, $pdf->GetY() + 4, 75, 25);
		}
	}else{
		$pdf->Image("./picture/signature.png", 70, $pdf->GetY() + 40, 75, 25 );
	}
	
	
	$pdf->Output("patient_report.pdf", "I");
?>
